package com.topboya.linker.ui.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.topboya.linker.R;
import com.topboya.linker.diyview.CenteredEditText;
import com.topboya.linker.model.response.LatelyDataResponse;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.adapter.SeeProductAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.ProduceInfoPresenter;
import com.topboya.linker.ui.view.ProduceInfoView;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class SeeProductInforActivity extends BaseActivity<ProduceInfoView, ProduceInfoPresenter> implements ProduceInfoView {

    private static final String TAG = "SellectProductInforActivity:";
    private Toolbar toolbar;
    private RecyclerView recycle_produceinfo;
    private CenteredEditText et_search_devices;
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private SmartRefreshLayout smart_refreshlayout;
    private RelativeLayout rl_selectall;
    private CheckBox ck_selectall;
    private  ArrayList<SelectFileResponse.FileList> selectFileList;
    private ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists;
//    上次选中的状态，在下次还要被选中
    private ArrayList<ProductInforListReponse.ProductDetailInfoList> productoListslateselect;
    private SeeProductAdapter selectProductAdapter;
    private int pageNo =1;
    private boolean isearch;
    private String position;
    @Override
    protected int getLayoutId() {

        return R.layout.activity_see_productinfo;
    }

    @Override
    protected void initView() {
        isearch = false;
        toolbar = findViewById(R.id.toolbar);
        recycle_produceinfo = findViewById(R.id.recycle_produceinfo);
        toolbar_title = findViewById(R.id.toolbar_title);
        smart_refreshlayout = findViewById(R.id.smart_refreshlayout);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        smart_refreshlayout.setEnableRefresh(false);//禁止刷新
        ck_selectall = findViewById(R.id.ck_selectall);
        et_search_devices = findViewById(R.id.et_search_devices);
        rl_selectall = findViewById(R.id.rl_selectall);
    }

    @Override
    protected void initData() {

        toolbar_title.setText("选择产品");
        tv_toolbar_right.setVisibility(View.GONE);
        selectFileList =new ArrayList<>();
        productDetailInfoLists = new ArrayList<>();
        productoListslateselect=(ArrayList<ProductInforListReponse.ProductDetailInfoList>) getIntent().getSerializableExtra("productDetailInfoList");
//        LogUtils.v(TAG + "productoListslateselect:"+productoListslateselect.toString());
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//      为代  item下面横线向目前是在条目布局加了一个view，后面改码设置
        selectProductAdapter = new SeeProductAdapter(this,productDetailInfoLists);
        recycle_produceinfo.setLayoutManager(layoutManager);
        recycle_produceinfo.setAdapter(selectProductAdapter);
        mPresenter.getProductInforList(String.valueOf(pageNo),null,"1");



    }
    public static void hideKeyboard(View view) {

        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    @Override
    protected void initListener() {

        et_search_devices.setOnEditorActionListener((v, actionId, event) -> {

            isearch = true;
            String searchkey = v.getText().toString().trim();
            mPresenter.getProductInforList(String.valueOf(pageNo),searchkey,"1");
            hideKeyboard(v);

            return false;
        });

        et_search_devices.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count ==0){
                    isearch =false;
                    mPresenter.getProductInforList("0",null,"1");
//                    hideKeyboard(et_search_devices);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {}
        });

        toolbar.setNavigationOnClickListener(v -> back());
         smart_refreshlayout.setOnLoadmoreListener(refreshlayout -> {

             pageNo++;
             mPresenter.getProductInforList(String.valueOf(pageNo),null,"1");

         });

    }

    @Override
    protected ProduceInfoPresenter createPresenter() {

        return new ProduceInfoPresenter(this);
    }


    @Override
    public void setProductInfoCustomeData(ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists,String count) {
        LogUtils.v(TAG+ "productDetailInfoLists:"+productDetailInfoLists.toString());

        if (!isearch ){
            if (pageNo==1){
                this.productDetailInfoLists.clear();
            }
            this.productDetailInfoLists.addAll(productDetailInfoLists);
            selectProductAdapter.notifyDataSetChanged();
            smart_refreshlayout.finishLoadmore();

        }else {

            this.productDetailInfoLists.clear();
            this.productDetailInfoLists.addAll(productDetailInfoLists);

            selectProductAdapter.notifyDataSetChanged();

        }

    }

    @Override
    public void setLatelyData(List<LatelyDataResponse.Data> latelyData) {

    }

    @Override
    public void onBackPressed() {

        back();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    private void back() {

         finish();
    }


}